import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart';
import 'package:goumao_app/Controller/gm_mine_edit_name.dart';
import 'package:goumao_app/Controller/gm_mine_edit_qianming.dart';
import 'package:goumao_app/Customs/gm_app_util.dart';
import 'package:goumao_app/Manager/gm_user_manager.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart'
    // ignore: library_prefixes
    as iOSDatePicker;

class GMUserProfilePage extends StatefulWidget {
  const GMUserProfilePage({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _GMUserProfilePageState createState() => _GMUserProfilePageState();
}

class _GMUserProfilePageState extends State<GMUserProfilePage> {
  late File _avatar;
  bool _upload = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: GMAppUtil.navText("个人资料"),
        elevation: 0,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            InkWell(onTap: _getImage, child: _getAvatar()),
            const Divider(
              height: 1,
            ),
            _getItem('名字', GMUserManager().user!.nick, () {
              Navigator.push(context,
                  CupertinoPageRoute(builder: (BuildContext context) {
                return const MyDataName();
              }));
            }),
            const Divider(
              height: 1,
            ),
            _getItem('性别', GMUserManager().user!.sex, () {
              GMAppUtil.gmToast("性别一旦设置，则不可修改");
            }),
            const Divider(
              height: 1,
            ),
            _getItem('常住地', GMUserManager().user!.city, () async {
              var result = await CityPickers.showCityPicker(
                context: context,
              );

              setState(() {
                GMUserManager().user!.city =
                    (result!.provinceName! + result.cityName!); // areaName
              });
            }),

            const Divider(
              height: 1,
            ),

            _getItem('生日', GMUserManager().user!.birthday, () {
              _showBirthdayPicker(context);
            }),
            const Divider(
              height: 1,
            ),
            _getSignature(), // 签名
            const Divider(
              height: 1,
            ),
          ],
        ),
      ),
    );
  }

  Widget _getSignature() {
    return InkWell(
        onTap: () {
          Navigator.push(context,
              MaterialPageRoute(builder: (BuildContext context) {
            return GMMineSignPage();
          }));
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 80,
              alignment: Alignment.topLeft,
              margin: const EdgeInsets.only(top: 15, left: 10, bottom: 15),
              child: const Text(
                '个性签名',
                style: TextStyle(color: Colors.grey),
              ),
            ),
             Expanded(
                child: Padding(
              padding: const EdgeInsets.only(left: 20, top: 10, bottom: 10),
              child: Text(
                GMUserManager().user!.sign,
                maxLines: 7,
                overflow: TextOverflow.ellipsis,
              ),
            )),
            const Icon(
              Icons.chevron_right,
              color: Colors.grey,
            ),
          ],
        ));
  }

    void _showBirthdayPicker(BuildContext context) {
    iOSDatePicker.DatePicker.showDatePicker(
      context,
      pickerMode: iOSDatePicker.DateTimePickerMode.date,
      minDateTime: DateTime(2000, 1, 1),
      maxDateTime: DateTime(
          DateTime.now().year, DateTime.now().month, DateTime.now().day),
      initialDateTime: DateTime.now(),
      dateFormat: "yyyy年,MM|dd",
      pickerTheme: const iOSDatePicker.DateTimePickerTheme(
        confirm: Text(
          "完成",
          style: TextStyle(
              color: Color(0xff73ed94),
              fontSize: 15,
              fontWeight: FontWeight.w400),
        ),
      ),
      onChange: (datetime, selectedIndex) {
        // setState(() {
        //   birthday = DateFormat('yyyy-MM-dd').format(datetime);
        // });
      },
    );
  }

  Widget _getAvatar() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Container(
          width: 60,
          height: 60,
          margin: const EdgeInsets.only(top: 15, left: 10, bottom: 15),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(6),
            child: !_upload
                ? Image.network(
                    GMUserManager().user!.icon,
                  )
                : Image.file(
                    _avatar,
                    fit: BoxFit.cover,
                  ),
          ),
        ),
        const Expanded(
            child: Padding(
          padding: EdgeInsets.only(left: 40),
          child: Text(
            '修改头像',
            style: TextStyle(color: Colors.grey),
          ),
        )),
        const Icon(
          Icons.chevron_right,
          color: Colors.grey,
        ),
      ],
    );
  }

  Widget _getItem(String lable, String value, onSelect) {
    return InkWell(
        onTap: () {
          onSelect();
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 80,
              margin: const EdgeInsets.only(top: 15, left: 10, bottom: 15),
              child: Text(
                lable,
                style: const TextStyle(color: Colors.grey),
              ),
            ),
            Expanded(
                child: Padding(
              padding: const EdgeInsets.only(left: 20),
              child: Text(
                value,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            )),
            const Icon(
              Icons.chevron_right,
              color: Colors.grey,
            ),
          ],
        ));
  }

  // 打开图片浏览器
  Future _getImage() async {
    final picker = ImagePicker();
    final pickedFile = await picker.pickImage(source: ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _avatar = File(pickedFile.path);
        _upload = true;
      } else {}
    });
  }
}
